//package com.cardetective.shopcar.page.activity.main.shopcar
//
//import android.annotation.SuppressLint
//import android.graphics.Color
//import android.os.Bundle
//import android.view.View
//import androidx.core.view.isVisible
//import androidx.lifecycle.Lifecycle
//import androidx.lifecycle.lifecycleScope
//import androidx.lifecycle.repeatOnLifecycle
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.transition.Visibility
//import com.android.basemvvm.BaseVMFragment
//import com.app.ctzoverseas.page.main.home.ShopCarViewModel
//import com.blankj.utilcode.util.BarUtils
//import com.blankj.utilcode.util.LogUtils
//import com.blankj.utilcode.util.ToastUtils
//import com.cardetective.shopcar.R
//import com.cardetective.shopcar.adapter.ShopCarStoreAdapter
//import com.cardetective.shopcar.common.Constant
//import com.cardetective.shopcar.common.GoodsCallback
//import com.cardetective.shopcar.databinding.FragmentShopcarBinding
//import com.cardetective.shopcar.network.model.CarGoodsData
//import com.cardetective.shopcar.network.model.CarStoreData
//import com.cardetective.shopcar.network.model.ShopCarInfoModel
//import com.cardetective.shopcar.utils.RouterUtil
//import com.google.gson.Gson
//import com.kongzue.dialogx.dialogs.CustomDialog
//import com.kongzue.dialogx.interfaces.OnBindView
//import kotlinx.coroutines.launch
//import okhttp3.internal.toImmutableList
//
//
///**
// * 购物车
// */
//class ShopCarFragment01 : BaseVMFragment<ShopCarViewModel, FragmentShopcarBinding>(), GoodsCallback {
//
//    private val shopCarAdapter by lazy { ShopCarStoreAdapter(this) }
////    val shopCarInfo =
////        Gson().fromJson<ShopCarInfoModel>(Constant().CAR_JSON, ShopCarInfoModel::class.java)
//    private var isChoiceAll: Boolean = false  //是否全选
//    private var shopIdList = mutableListOf<Int>() // 记录选中店铺id
//    private var totalPrice: Double = 0.0 // 商品总价格
//    private var totalCount: Int = 0 // 商品总数量
//
//
//    @SuppressLint("NotifyDataSetChanged")
//    override fun initView(savedInstanceState: Bundle?) {
//        super.initView(savedInstanceState)
//        mBinding?.let {
//            it.llTopMoreInfo.setPadding(
//                0,
//                BarUtils.getStatusBarHeight(),
//                0,
//                0
//            )
//            setOnViewClick(
//                it.clBtnAllChoice,
//                it.clBtnEdit,
//                it.tvBtnBottomAll,
//                it.btnShare,
//                it.btnDelete,
//                it.llSubmitOrder
//            )
//            it.llCarGoodsLayout.rcvList.layoutManager = LinearLayoutManager(context)
//            it.llCarGoodsLayout.rcvList.adapter = shopCarAdapter
//
//            shopCarAdapter.setOnItemChildClickListener { adapter, view, position ->
//                var storeInfo = shopCarInfo.orderData[position] as CarStoreData
//
//                when (view.id) {
//                    R.id.iv_choice_store -> {
//                        storeInfo.isChecked = !storeInfo.isChecked
//                        shopCarAdapter.notifyDataSetChanged()
//                        // 点击了选中店铺，里面的商品也随之选中，反之亦然
//                        if (storeInfo.isChecked) {
//                            shopCarAdapter.controlGoods(storeInfo.shopId, true)
//
//                            // 添加到列表
//                            if (!shopIdList.contains(storeInfo.shopId)) {
//                                shopIdList.add(storeInfo.shopId)
//                            }
//                        } else {
//                            shopCarAdapter.controlGoods(storeInfo.shopId, false)
//                            //从列表中清楚
//                            if (shopIdList.contains(storeInfo.shopId)) {
//                                shopIdList.remove(storeInfo.shopId)
//                            }
//                        }
//                        controlAllChecked(shopCarAdapter.data)
//                    }
//
//                    R.id.tv_store_name -> {
//                        ToastUtils.showShort("进入店铺")
//                    }
//                }
//            }
//
//            // 获取购物车
//            viewModel.getCarData()
//        }
//    }
//
//    override fun initObserve() {
//        super.initObserve()
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.CREATED) {
//                launch {
//                    viewModel.carList.collect {
//                        val cartList = it.cartList
//                        shopCarAdapter.setList(cartList)
//
//                    }
//                }
//            }
//        }
//    }
//
//
//    override fun onClickSlow(view: View?) {
//        super.onClickSlow(view)
//        when (view) {
//            mBinding?.clBtnAllChoice -> { // 顶部全部
//                isSelectAllStore(!isChoiceAll)
//            }
//
//            mBinding?.clBtnEdit -> { // 顶部编辑
//                mBinding?.llTopMoreLayout?.isVisible = !mBinding?.llTopMoreLayout?.isVisible!!
//                mBinding?.llTopManageLayout?.isVisible = !mBinding?.llTopManageLayout?.isVisible!!
//                mBinding?.llBalanceBottomLayout?.isVisible =
//                    !mBinding?.llBalanceBottomLayout?.isVisible!!
//                mBinding?.clEditBottomLayout?.isVisible = !mBinding?.clEditBottomLayout?.isVisible!!
//
//            }
//
//            mBinding?.tvBtnBottomAll -> { // 底部全部
//
//            }
//
//            mBinding?.btnShare -> { // 分享
//
//            }
//
//            mBinding?.btnDelete -> { // 删除
//                showDeleteGoodsView()
//            }
//
//            mBinding?.llSubmitOrder -> { //提交订单
//                RouterUtil.goSubmitOrderPage()
//            }
//        }
//    }
//
//    // 选中店铺
//    @SuppressLint("NotifyDataSetChanged")
//    override fun checkedStore(shopId: Int, state: Boolean) {
//        val storeDataList = shopCarAdapter.data
//        for (storeInfo in storeDataList) {
//            if (shopId == storeInfo.shopId) {
//                storeInfo.isChecked = state
//                shopCarAdapter.notifyDataSetChanged()
//                // 记录选中店铺的shopid,添加到列表中
//                if (!shopIdList.contains(shopId) && state) {
//                    //如果列表中没有这个店铺Id且当前店铺为选中状态
//                    shopIdList.add(shopId)
//                } else {
//                    if (shopIdList.contains(shopId)) {
//                        shopIdList.remove(shopId)
//                    }
//                }
//            }
//        }
//        controlAllChecked(storeDataList)
//    }
//
//
//    private fun controlAllChecked(storeDataList: MutableList<CarStoreData>) {
//        if (shopIdList.size == storeDataList.size && storeDataList.size != 0) {
//            // 全选
//            mBinding?.ivTopBtnAll?.setImageResource(R.mipmap.icon_shop_check_press)
//            isChoiceAll = true
//        } else {
//            // 全不选
//            mBinding?.ivTopBtnAll?.setImageResource(R.mipmap.icon_shop_check_normal)
//            isChoiceAll = false
//        }
//        // 计算价格
//        calculationPrice()
//    }
//
//    // 计算价格
//    override fun calculationPrice() {
//        //每次计算之前先置零
//        totalPrice = 0.00
//        totalCount = 0
//        //循环购物车中的店铺列表
//        val storeDataList = shopCarAdapter.data
//        if (storeDataList.isNotEmpty()) {
//            for (storeItem in storeDataList) {
//                for (goodsItem in storeItem.cartlist) {
//                    if (goodsItem.isChecked) {
//                        totalCount++
//                        totalPrice += goodsItem.price * goodsItem.count
//                    }
//                }
//            }
//        }
//        // 设置总价格
//        mBinding?.tvTotalPrice?.text = "$$totalPrice"
//    }
//
//    // 选择规格
//    override fun checkSpecifications() {
//        showChoiceGoodsSpecificationsView()
//    }
//
//    // 是否全选
//    private fun isSelectAllStore(state: Boolean) {
//        mBinding?.ivTopBtnAll?.setImageResource(if (state) R.mipmap.icon_shop_check_press else R.mipmap.icon_shop_check_normal)
//        val storeDataList = shopCarAdapter.data
//        if (storeDataList.isNotEmpty()) {
//            for (store in shopCarAdapter.data) {
//                // 商品是否选中
//                shopCarAdapter.controlGoods(store.shopId, state)
//                // 店铺是否选中
//                checkedStore(store.shopId, state)
//            }
//        }
//        isChoiceAll = state
//    }
//
//    // 删除商品弹框
//    private fun showDeleteGoodsView() {
//        CustomDialog.show(object : OnBindView<CustomDialog>(R.layout.dialog_delete_cart_goods) {
//            override fun onBind(dialog: CustomDialog, v: View) {
//                v.findViewById<View>(R.id.btn_delete).setOnClickListener {
//                    dialog.dismiss()
//                    deleteGoods()
//                }
//                v.findViewById<View>(R.id.btn_cancle).setOnClickListener {
//                    dialog.dismiss()
//                }
//            }
//        }).setAlign(CustomDialog.ALIGN.CENTER)
//            .setMaskColor(Color.parseColor("#4D000000"))
//    }
//
//    // 删除商品
//    @SuppressLint("NotifyDataSetChanged")
//    private fun deleteGoods() {
//        var storeList = mutableListOf<CarStoreData>()
//        var storeAdapterList = shopCarAdapter.data.toMutableList()
//        for (storeInfo in storeAdapterList) {
//            if (storeInfo.isChecked) {
//                storeList.add(storeInfo)
//            }
//
//            var goodsList = mutableListOf<CarGoodsData>()
//            var goodsAdapterList = storeInfo.cartlist.toMutableList()
//            for (goodsInfo in goodsAdapterList) {
//                if (goodsInfo.isChecked) {
//                    goodsList.add(goodsInfo)
//                }
//            }
//            // 移除列表中的数据
//            goodsAdapterList.removeAll(goodsList)
//            LogUtils.e("多大的")
//        }
//        //移除店铺
//        storeAdapterList.removeAll(storeList)
//        // 清空选中的店铺id集合
//        shopIdList.clear()
//        // 控制全选
//        controlAllChecked(storeAdapterList)
//        // 刷新列表
//        shopCarAdapter.notifyDataSetChanged()
//    }
//
//    // 选择规格弹框
//    private fun showChoiceGoodsSpecificationsView() {
//        CustomDialog.show(object :
//            OnBindView<CustomDialog>(R.layout.dialog_cart_goods_specifications) {
//            override fun onBind(dialog: CustomDialog, v: View) {
//
//            }
//        }).setAlign(CustomDialog.ALIGN.BOTTOM)
//            .setMaskColor(Color.parseColor("#4D000000"))
//    }
//
//}